package com.chenyun.infra.monitor.controller;

import com.chenyun.common.vo.AjaxResult;
import com.chenyun.infra.monitor.vo.RedisMonitorVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Properties;

/**
 * RedisMonitorController：
 * Description:
 *
 * @author: Lonni
 * @date: 2024/1/12 0012 15:20
 */
@RestController
@RequestMapping("monitor")
public class MonitorController implements Serializable {


    @Resource
    @Lazy
    private RedisTemplate redisTemplate;


    @GetMapping("redis/info")
    @ApiOperation("获得 Redis 监控信息")
    public AjaxResult getRedisMonitorInfo() {
        // 获得 Redis 统计信息
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
        Long dbSize = (Long) redisTemplate.execute(RedisServerCommands::dbSize);
        Properties commandStats = (Properties) redisTemplate.execute((
                RedisCallback<Properties>) connection -> connection.info("commandstats"));
        assert commandStats != null;
     return AjaxResult.SUCCESS(RedisMonitorVO.convertRedisVO(info, dbSize, commandStats));
    }









}



